#include <Windows.h>
#include "CCheckPacket.h"
#include "CLinkshell.h"
#include "CItemContainer.h"

CCheckPacketEquip::CCheckPacketEquip(CChar * schar, int * equipCnt) {

	this->type = 0xC9;
	this->size = 0x06;
	memset(this->data, 0,  sizeof(this->data));

	memcpy(this->data+0x04-0x04, &(schar->m_id), sizeof(schar->m_id));
	unsigned short targId = schar->getTargId();
	memcpy(this->data+0x08-0x04, &targId, 2);

	this->data[0x0A-0x04] = 0x03;

	int count = 0, i;
	memcpy(&i, equipCnt, sizeof(int));
	do {
		unsigned char invSlotID = schar->equip[i];
		CItem * item;
		item = schar->getItem(LOC_INVENTORY, invSlotID);
		if(item != NULL){
			unsigned short itemID = item->getId();
			memcpy((this->data + (this->size*2) - 0x04), (char *)&(itemID), sizeof(short));
			data[(this->size*2) + 0x02 - 0x04] = i;
			this->size += 0x0E;
			count++;
		}
		i++;
		memcpy(equipCnt, &i, sizeof(int));
	} while ((count < 8) && (i < 16));

	if(count == 0)
		this->size = 0x14;

	data[0x0B-0x04] = count;

}

CCheckPacketEquip::~CCheckPacketEquip() {

}

CCheckPacket::CCheckPacket(CChar * schar, CChar * target) {

	int equipCnt = 0;
	// 
	schar->quePacket(CCheckPacketEquip(target, &equipCnt));
	if(equipCnt < 16)
		schar->quePacket(CCheckPacketEquip(target, &equipCnt));

	this->type = 0xC9;
	this->size = 0x28;
	memset(this->data, 0,  sizeof(this->data));

	memcpy(this->data+0x04-0x04, &(target->m_id), sizeof(target->m_id));
	unsigned short targId = target->getTargId();
	memcpy(this->data+0x08-0x04, &targId, 2);

	this->data[0x0A-0x04] = 0x01;

	CLinkshellItem * targetLSItem = target->getLinkshellItem();
	CLinkshell * targetLS = target->getLinkshell();
	if(targetLS != NULL) {
		unsigned long lsID = targetLS->getLsId();
		unsigned long lsItemID = 0x0000;
		if(targetLSItem != NULL)
			lsItemID = targetLSItem->getId();
		unsigned short lsColor = targetLS->getLsColor();
	
		memcpy(this->data+0x0C-0x04, &lsID, 4);
		memcpy(this->data+0x0E-0x04, &lsItemID, 4);
		memcpy(this->data+0x10-0x04, &lsColor, 2);
	
		char * lsName = targetLS->getLsName();
		if(lsName != NULL)
			CLinkshell::compressLsName(lsName, (char*)data+0x14-0x04);	}

	if(!(target->h.flags & FLAG_ANON)){
		data[0x12-0x04] = target->m_jobMain;
		data[0x13-0x04] = target->m_jobSub;
		data[0x23-0x04] = target->m_lvlMain;
		data[0x24-0x04] = target->m_lvlSub;
	}
	
	this->data[0x2C - 0x04] = 0x00;  //Ballista Star next to Chevron count
	//Chevron 32 bit Big Endean, starting at 0x2B

}

CCheckPacket::~CCheckPacket() {

}
